package it.webrules.deploymanager.core.dy;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class VersioningUtils {

	private static Pattern compile = Pattern.compile("(\\d+)[^\\d]*$");

	private VersioningUtils() {
	}

	public static String updateVersion(String version) {

		if (version == null)
			return "1.0.0-SNAPSHOT";

		Matcher matcher = compile.matcher(version);
		if (!matcher.find())
			return version;

		String ver = matcher.group(matcher.groupCount());
		ver = String.valueOf(Integer.valueOf(ver) + 1);
		int start = matcher.start(matcher.groupCount());
		int end = matcher.end(matcher.groupCount());

		StringBuilder sb = new StringBuilder(version);
		sb.delete(start, end);
		sb.insert(start, ver);
		return sb.toString();

	}

}
